<script type="text/javascript">
	//记录下拉框选择项
	var modules_id = 0;
	//记录当前等级
	var level = 1;
	//记录父节点
	var parent_id ;
	
	var value_meaningName = "";
	$(function (){
		$("#list_date").bootstrapTreeGrid({
			striped: true, 
		    url: global_param.context_name + '/sys/getDictAllList.do',
		    sidePagination : 'server',
		    cache: false,
		    queryParams: queryParams,
		    treeView: true,
            treeId: "sys_dict_id",
            treeParentId: "p_sys_dict_id",
            treeField: "value_meaning",
            clickToSelect: true,
            treeRootLevel: 1,
	        height:510,
	        formatNoMatches: function () {  //没有匹配的结果  
	            return '无符合条件的记录';  
	        }, 
	        formatLoadingMessage: function(){
	            return "请稍等，正在加载中。。。";
	        },
		    columns: [
		    	{
		    		field: 'dict_level',
		            title: '序号',
		            switchable:false,
		    		align:"center",
			    	valign:"middle",
		            formatter: function (value, row, index) {
		                return index+1;  
		            }
		        },
		        {
			    	field:"",
			    	checkbox: true,
			    	title:"选择",
			    	class:"tablebody",
			    	align:"center",
			    	valign:"middle"
			    },
			    {
		            field : 'value_meaning',
		            title : '数据名称 ',
		            width :'400px',
// 			    	align:"center",
			    	valign:"middle"
		        },
			    {
		            field : 'key_code',
		            title : '数据编码',
		            width :'400px',
			    	align:"center",
			    	valign:"middle"
		        },
		        {
		            field : 'sort_order',
		            title : '排序序号',
		            width :'230px',
			    	align:"center",
			    	valign:"middle"
		        },
			    {
		            field : 'create_timestamp_str',
		            title : '创建时间',
		            width :'230px',
			    	align:"center",
			    	valign:"middle"
		        },
			    {
		            field : 'remark',
		            title : '备注',
		            width :'230px',
			    	align:"center",
			    	valign:"middle"
		        }]
		});
		
		$('#list_date').bootstrapTreeGrid('hideColumn', 'sys_dict_id');
		$('#list_date').bootstrapTreeGrid('hideColumn', 'p_sys_dict_id'); 	
		
		//回车查询
	    $(window).keydown(function(event){
	    	if (event.keyCode == 13) {
	    		FindData();
	        }
	    });
	});

	function queryParams(params) {
	    var param = {
	    		key_code : $('#key_code1').val(),
	    		value_meaning :$('#value_meaning1').val()
	    }
	    return param;
	}
	
	//查询列表
	function FindData() {
		$("#list_date").bootstrapTable("refresh",{});
	}

function addDictData(){
	var array = $("#list_date").bootstrapTreeGrid('getSelections');
	if(array.length <= 1){
		if(array.length == 0){
			value_meaningName = "	";			
		}else{
			
			value_meaningName = array[0].value_meaning;
		}
	    BootstrapDialog.show({
	    	title: '添加业务数据',
	    	titleClass: 'text-center',
	        message: $('<div></div>').load('static/html/systemManage/addDict.html'),
	        buttons: [{
	            label: '保存',
	            cssClass:'btn btn-primary col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
	            action: function(dialog) {
	        		if($(dialog.$modalBody).find('#value_meaning').val() == ''){
	        			globalUtil.showSuccessMessage('请填写业务数据名称!');
	        		}else if($(dialog.$modalBody).find('#key_code').val() == ''){
	        			globalUtil.showSuccessMessage('请填写业务数据编码!');
	        		}else if($(dialog.$modalBody).find('#sort_order').val() == ''){
	        			globalUtil.showSuccessMessage('请填写业务数据序号!');
	        		}else{
	        			if($('#add_dict_form').data("bootstrapValidator").isValid()){
	            		add_perm(dialog);
	        			}
	        		}
	            }
	        }, {
	            label: '关闭',
	            cssClass:'btn btn-default col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
	            action: function(dialog) {
	                dialog.close();
	            }
	        }]
	    });
	}else{
		globalUtil.showSuccessMessage("请选择一条数据!");
	}
}
	
function updateDict(){
	var array = $("#list_date").bootstrapTreeGrid('getSelections');
	 if(array.length == 1){
	    BootstrapDialog.show({
	    	title: '更新业务数据',
	    	titleClass: 'text-center',
	        message: function(dialog) {
		        	var $content = $('<div></div>').load('static/html/systemManage/updateDict.html');
		        	return $content;
	        },
	        buttons: [{
			            label: '保存',
			            cssClass:'btn btn-primary col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
			            action: function(dialog) {
			            	if($(dialog.$modalBody).find('#value_meaning').val() == ''){
			            		globalUtil.showSuccessMessage('请填写业务数据名称!');
			        		}else if($(dialog.$modalBody).find('#key_code').val() == ''){
			        			globalUtil.showSuccessMessage('请填写业务数据编码!');
			        		}else if($(dialog.$modalBody).find('#sort_order').val() == ''){
			        			globalUtil.showSuccessMessage('请填写业务数据序号!');
			        		}else{
// 			        			if($('#update_perm_form').data("bootstrapValidator").isValid()){
			        				updateDictData(dialog);
// 				        			}
			        		}
			            }
			        }, {
			            label: '关闭',
			            cssClass:'btn btn-default col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
			            action: function(dialog) {
			                dialog.close();
			            }
			        }]
	    });
	}else{
		globalUtil.showSuccessMessage("请选择一条数据!");
	}
	
}

//添加方法
function add_perm(obj){
	var rows = $("#list_date").bootstrapTreeGrid('getSelections');
	if(rows.length==0){
		parent_id=0;
		level=1;
		
	}
	if(rows.length==1){
			parent_id=rows[0].sys_dict_id;
			level=rows[0].dict_level+1;
		
	}
    $.ajax({
    	data : {
    		value_meaning : $(obj.$modalBody).find('#value_meaning').val(),
    		key_code : $(obj.$modalBody).find('#key_code').val(),
    		sort_order : $(obj.$modalBody).find('#sort_order').val(),
    		remark : $(obj.$modalBody).find('#remark').val(),
 			level : level,
 			parent_id : parent_id
    	},
    	async: false,
    	url : global_param.context_name+"/sys/saveDictData.do", 
		type : 'post',  				    
		success: function(data){   
			if(data=='success'){
				toastr.success("添加成功!");
				obj.close();
				$("#list_date").bootstrapTable("refresh",{});
			 }else if(data=="key_codeRepeat"){
				 globalUtil.showSuccessMessage('数据编码重复,请修改!');
			 }else{
				 globalUtil.showSuccessMessage('添加失败!'); 
			 }
		 }  
  }); 	
}
	
function updateDictData(obj){
	
	var rows = $("#list_date").bootstrapTreeGrid('getSelections');
	if(rows.length==0){
		parent_id=0;
		level=1;
		
	}
	if(rows.length==1){
			parent_id=rows[0].p_sys_dict_id;
			level=rows[0].dict_level;
		
	}
	
    $.ajax({
    	data:{
    		value_meaning : $(obj.$modalBody).find('#value_meaning').val(),
    		key_code : $(obj.$modalBody).find('#key_code').val(),
    		sort_order : $(obj.$modalBody).find('#sort_order').val(),
    		remark : $(obj.$modalBody).find('#remark').val(),
    		sys_dict_id :$(obj.$modalBody).find('#sys_dict_id').val(),
    		level : level,
 			parent_id : parent_id
    	},
    	async: false,
    	url : global_param.context_name+"/sys/updateDictData.do", 
		type : 'post',  				    
		success: function(data){
			if(data=='success'){
				toastr.success("修改成功!");
				obj.close();
				$("#list_date").bootstrapTable("refresh",{});
			 }else if(data=="key_codeRepeat"){
				 globalUtil.showSuccessMessage('数据编码重复,请修改!');
			 }else{
				 globalUtil.showSuccessMessage('修改失败!'); 
			 }
		 }  
  }); 	
}
// 练习
function operTionDict(){
	var array = $("#list_date").bootstrapTreeGrid('getSelections');
	 if(array.length == 1){
	    BootstrapDialog.show({
	    	title: '更新业务数据',
	    	titleClass: 'text-center',
	        message: function(dialog) {
		        	var $content = $('<div></div>').load('static/html/testHtml/test.html');
		        	return $content;
	        },
	        buttons: [{
			            label: '保存',
			            cssClass:'btn btn-primary col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
			            action: function(dialog) {
			            	if($(dialog.$modalBody).find('#value_meaning').val() == ''){
			            		globalUtil.showSuccessMessage('请填写业务数据名称!');
			        		}else if($(dialog.$modalBody).find('#key_code').val() == ''){
			        			globalUtil.showSuccessMessage('请填写业务数据编码!');
			        		}else if($(dialog.$modalBody).find('#sort_order').val() == ''){
			        			globalUtil.showSuccessMessage('请填写业务数据序号!');
			        		}else{
//			        			if($('#update_perm_form').data("bootstrapValidator").isValid()){
			        				updateDictData(dialog);
//				        			}
			        		}
			            }
			        }, {
			            label: '关闭',
			            cssClass:'btn btn-default col-sm-1 col-sm-offset-4 col-xs-4 col-xs-offset-3',
			            action: function(dialog) {
			                dialog.close();
			            }
			        }]
	    });
	}else{
		globalUtil.showSuccessMessage("请选择一条数据!");
	}
}
	//删除
function deleteDict(){
	var rows = $("#list_date").bootstrapTreeGrid('getSelections');//选中节点并返回
	var  temp = [];
	if(rows.length>0){
		globalUtil.showConfirmMessage('您确认想要删除此业务数据及其子节点吗？',function(){
		    	for(var i=0;i<rows.length;i++){
		    		var tempbol = true;
		    		for(var c=0;c<temp.length;c++){
		    			if(temp[c] == rows[i].sys_dict_id){
		    				tempbol = false;
		    				break;
		    			}
		    		}
		    		if(tempbol){
		    			temp.push(rows[i].sys_dict_id);
		    		}
		    		
		    		var chil = getDictChildren(rows[i]);
		    		for(var j=0;j<chil.length;j++){
		    		     var bol = true;
		    		     for(var k=0;k<temp.length;k++){
		    		    	 if(temp[k] == chil[j].sys_dict_id){
		    		    		 bol = false;
		    		    		 break;
		    		    	 }
		    		     }
		    		     if(bol){
		    		    	 temp.push(chil[j].sys_dict_id);
		    		     }
		    		} 
		    	}    
		 		$.ajax({
					url : global_param.context_name+"/sys/delDictData.do", 
					type : 'post',
					async: false,
					data : {
						idList : temp.join(',')
					},
					cache : false,
					success: function(data){    
					if(data=='success'){
						toastr.success("删除成功!");
						$("#list_date").bootstrapTable("refresh",{});
					}
			    }  
			  });
		}); 
	}
	else{
		globalUtil.showSuccessMessage('请选择要删除的记录!');
	}
}
	
function getDictChildren(row){
	let children = [];
	let datas = $("#list_date").bootstrapTreeGrid('getData');
	let flg = false;
	for(let i = 0;i<datas.length;i++){
		if(row.sys_dict_id == datas[i].sys_dict_id){
			flg = true;
			continue;
		}
		if(flg){
			if(row.dict_level >= datas[i].dict_level){
				break;
			}else{
				children.push(datas[i]);
			}
		}
	}
	
	return children;
}


function clearQueryParams() {
	$("#key_code1").val('');
	$("#value_meaning1").val('');
}
</script>

<section class="content-header">
	<h1 class="page-name">数据维护</h1>
	<ol class="breadcrumb">
	</ol>
</section>
<!-- Main content -->
<section class="content item">
	 <div class="container-fluid">
		<div class="row">
			<div class="box">
        		<div class="box-body">
        			<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
		              <div class="form-group">
		              	<label for="key_code1">数据编码</label>
		                <input type="text" class="form-control"  id="key_code1" name="key_code1" style="width: 100%;" placeholder="请输入数据编码" >
		              </div>
		            </div>
		            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
		            	<div class="form-group">
			                <label for="value_meaning1">数据名称</label>
							<input type="text" class="form-control"  id="value_meaning1" name="value_meaning1" style="width: 100%;" placeholder="请输入数据名称" >
		                </div>
		            </div>
		            <div class="col-lg-3 col-md-3 col-sm-5 col-xs-12">
		            	<div class="form-group">
		             	   <label>&nbsp;</label>
		             	   <div class="row">
							   <div class="col-xs-6">
								   <button type="button" class="btn btn-block btn-primary" onclick="FindData()">查询</button>
							   </div>
							   <div class="col-xs-6">
								   <button type="button" class="btn btn-block btn-default" onclick="clearQueryParams()">清空</button>
							   </div>
						   </div>
					    </div>
		            </div>
				</div>
			</div>
		   </div>
		</div>
    
    
    <div class="row">
        <div class="col-xs-12">
            <div class="box box-primary">
              <div class="box-header">
                 <i class="fa fa-list"></i>
                 <h3 class="box-title">数据列表</h3>
              </div>
				<div class="box-body">
					<div class="btn-group" id="toolZoom">
					    <button type="button" class="btn btn-default btn-lg" onclick="addDictData();"><i class="fa fa-plus"></i></button>
					    <button type="button" class="btn btn-default btn-lg" onclick="updateDict();"><i class="fa fa-edit"></i></button>
					    <button type="button" class="btn btn-default btn-lg" onclick="deleteDict();"><i class="fa fa-trash-o"></i></button>
					    <button type="button" class="btn btn-default btn-lg" onclick="operTionDict();"><i class="fa fa-trash-o"></i></button>
					</div>
					<table id="list_date" data-toolbar="#toolZoom">	
					</table>
				</div>
				<input type="hidden" id="changeId" name="changeId" value=""/>
		</div>
	</div>
</div>
</section>

